#include "etcd/Client.h"
#include "etcd/Client.hpp"
#include "etcd/SyncClient.hpp"
#include "etcd/Watcher.hpp"
#include "etcd/Response.hpp"
#include "etcd/Value.hpp"

// EtcdClient相关函数
EtcdClient* etcd_create(const char* etcd_url, const char* load_balancer) {
  return reinterpret_cast<EtcdClient*>(new etcd::Client(etcd_url, load_balancer));
}

void etcd_destroy(EtcdClient* client) {
  delete reinterpret_cast<etcd::Client*>(client);
}

EtcdResponse* etcd_get(EtcdClient* client, const char* key) {
  auto response = reinterpret_cast<etcd::Client*>(client)->get(key);
  return reinterpret_cast<EtcdResponse*>(new etcd::Response(response));
}

EtcdResponse* etcd_set(EtcdClient* client, const char* key, const char* value, int64_t leaseId) {
  auto response = reinterpret_cast<etcd::Client*>(client)->set(key, value, leaseId);
  return reinterpret_cast<EtcdResponse*>(new etcd::Response(response));
}

EtcdResponse* etcd_add(EtcdClient* client, const char* key, const char* value, int64_t leaseId) {
  auto response = reinterpret_cast<etcd::Client*>(client)->add(key, value, leaseId);
  return reinterpret_cast<EtcdResponse*>(new etcd::Response(response));
}

EtcdResponse* etcd_modify(EtcdClient* client, const char* key, const char* value, int64_t leaseId) {
  auto response = reinterpret_cast<etcd::Client*>(client)->modify(key, value, leaseId);
  return reinterpret_cast<EtcdResponse*>(new etcd::Response(response));
}

EtcdResponse* etcd_rm(EtcdClient* client, const char* key) {
  auto response = reinterpret_cast<etcd::Client*>(client)->rm(key);
  return reinterpret_cast<EtcdResponse*>(new etcd::Response(response));
}

// EtcdSyncClient相关函数
EtcdSyncClient* etcd_sync_create(const char* etcd_url, const char* load_balancer) {
  return reinterpret_cast<EtcdSyncClient*>(new etcd::SyncClient(etcd_url, load_balancer));
}

void etcd_sync_destroy(EtcdSyncClient* client) {
  delete reinterpret_cast<etcd::SyncClient*>(client);
}

EtcdResponse* etcd_sync_get(EtcdSyncClient* client, const char* key) {
  auto response = reinterpret_cast<etcd::SyncClient*>(client)->get(key);
  return reinterpret_cast<EtcdResponse*>(new etcd::Response(response));
}

EtcdResponse* etcd_sync_set(EtcdSyncClient* client, const char* key, const char* value, int64_t leaseId) {
  auto response = reinterpret_cast<etcd::SyncClient*>(client)->set(key, value, leaseId);
  return reinterpret_cast<EtcdResponse*>(new etcd::Response(response));
}

EtcdResponse* etcd_sync_add(EtcdSyncClient* client, const char* key, const char* value, int64_t leaseId) {
  auto response = reinterpret_cast<etcd::SyncClient*>(client)->add(key, value, leaseId);
  return reinterpret_cast<EtcdResponse*>(new etcd::Response(response));
}

EtcdResponse* etcd_sync_modify(EtcdSyncClient* client, const char* key, const char* value, int64_t leaseId) {
  auto response = reinterpret_cast<etcd::SyncClient*>(client)->modify(key, value, leaseId);
  return reinterpret_cast<EtcdResponse*>(new etcd::Response(response));
}

EtcdResponse* etcd_sync_rm(EtcdSyncClient* client, const char* key) {
  auto response = reinterpret_cast<etcd::SyncClient*>(client)->rm(key);
  return reinterpret_cast<EtcdResponse*>(new etcd::Response(response));
}

// EtcdWatcher相关函数
EtcdWatcher* etcd_watcher_create(EtcdClient* client, const char* key, void (*callback)(EtcdResponse*)) {
  auto watcher = new etcd::Watcher(*reinterpret_cast<etcd::Client*>(client), key, [callback](etcd::Response response) {
    callback(reinterpret_cast<EtcdResponse*>(new etcd::Response(response)));
  });
  return reinterpret_cast<EtcdWatcher*>(watcher);
}

void etcd_watcher_destroy(EtcdWatcher* watcher) {
  delete reinterpret_cast<etcd::Watcher*>(watcher);
}

// EtcdResponse相关函数
int etcd_response_error_code(EtcdResponse* response) {
  return reinterpret_cast<etcd::Response*>(response)->error_code();
}

const char* etcd_response_error_message(EtcdResponse* response) {
  return reinterpret_cast<etcd::Response*>(response)->error_message().c_str();
}

bool etcd_response_is_ok(EtcdResponse* response) {
  return reinterpret_cast<etcd::Response*>(response)->is_ok();
}

const char* etcd_response_action(EtcdResponse* response) {
  return reinterpret_cast<etcd::Response*>(response)->action().c_str();
}

int64_t etcd_response_index(EtcdResponse* response) {
  return reinterpret_cast<etcd::Response*>(response)->index();
}

EtcdValue* etcd_response_value(EtcdResponse* response) {
  return reinterpret_cast<EtcdValue*>(const_cast<etcd::Value*>(&reinterpret_cast<etcd::Response*>(response)->value()));
}

EtcdValue* etcd_response_prev_value(EtcdResponse* response) {
  return reinterpret_cast<EtcdValue*>(const_cast<etcd::Value*>(&reinterpret_cast<etcd::Response*>(response)->prev_value()));
}

// EtcdValue相关函数
const char* etcd_value_key(EtcdValue* value) {
  return reinterpret_cast<etcd::Value*>(value)->key().c_str();
}

const char* etcd_value_as_string(EtcdValue* value) {
  return reinterpret_cast<etcd::Value*>(value)->as_string().c_str();
}

int64_t etcd_value_created_index(EtcdValue* value) {
  return reinterpret_cast<etcd::Value*>(value)->created_index();
}

int64_t etcd_value_modified_index(EtcdValue* value) {
  return reinterpret_cast<etcd::Value*>(value)->modified_index();
}

int64_t etcd_value_version(EtcdValue* value) {
  return reinterpret_cast<etcd::Value*>(value)->version();
}

int etcd_value_ttl(EtcdValue* value) {
  return reinterpret_cast<etcd::Value*>(value)->ttl();
}

int64_t etcd_value_lease(EtcdValue* value) {
  return reinterpret_cast<etcd::Value*>(value)->lease();
}